package com.zjjt.dc.modules.datacatch.hdh.api;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import com.zjjt.dc.common.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class HDHApiUtil {
    public static void apiTest(Integer urlIndex){
            String appId = "5406301d7cdb4ab1a30701d325b68eab";
            String appSecret = "6105b6eff51f4fdeb07b7321a7345f10";
        if(ObjectUtil.isNotEmpty(urlIndex)) {
            HttpRequest request = HttpRequest.post(HDHApiConst.apiUrls[urlIndex]);
            JSONObject param = new JSONObject();
            param.set("appId", appId)
                    .set("page", 1)
                    .set("limit", 10)  // < 200
                    .set("logNo", Instant.now().toEpochMilli())
                    .set("prePageLastId", null);

            String sign = MD5Util.getMD5Format(param.toString() + appSecret);

            request.header("Content-Type", "application/json")
                    .header("sign", sign)
                    .body(param.toString());
            String response = "";
            Integer total = 0;
            try {
                response = request.execute().body();
                JSONObject jsonResponse = new JSONObject(response);
                total = (Integer) jsonResponse.getByPath("data.total");
            }catch (Exception e){
                throw new RuntimeException(e);
            }
            log.info("response:{}, total:{}", response, total);
         }else{
            HttpRequest request = HttpRequest.post(HDHApiConst.apiUrls[0]);
            JSONObject param = new JSONObject();
            param.set("appId", appId)
                    .set("page", 1)
                    .set("limit", 1)
                    .set("logNo", Instant.now().toEpochMilli())
                    .set("prePageLastId", null);

            String sign = MD5Util.getMD5Format(param.toString() + appSecret);

            request.header("Content-Type", "application/json")
                    .header("sign", sign)
                    .body(param.toString());
            StringBuilder sbResponse = new StringBuilder();
            Integer total = 0;
            try {
                sbResponse.append(request.execute().body());
                JSONObject jsonResponse = new JSONObject(sbResponse);
                total = (Integer) jsonResponse.getByPath("data.total");
            }catch (Exception e){
                throw new RuntimeException(e);
            }
            finally {
                request = null;
            }
            request = HttpRequest.post(HDHApiConst.apiUrls[0]);
            JSONObject param0 = new JSONObject();
            param.set("appId", appId)
                    .set("page", total)
                    .set("limit", 1)
                    .set("logNo", Instant.now().toEpochMilli())
                    .set("prePageLastId", null);

            sign = MD5Util.getMD5Format(param0.toString() + appSecret);

            request.header("Content-Type", "application/json")
                    .header("sign", sign)
                    .body(param0.toString());
            sbResponse = new StringBuilder();

            try {
                sbResponse.append(request.execute().body());
            }catch (Exception e){
                throw new RuntimeException(e);
            }
            finally {
                request = null;
            }

            log.info("response:{}", sbResponse);
        }
    }
}
